<?php
namespace app\ask\controller;

use think\Controller;

class Comment extends Controller
{
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->PostModel=model("Post.Post");
        $this->CateModel=model("Post.Category");
        $this->BusinessModel=model("Business.Business");
        $this->CommentModel=model("Post.Comment");
        $this->CollectModel=model("Post.Collect");

        $this->postid=$this->request->param('postid',0,'trim');
        $this->post=$this->PostModel->find($this->postid);
        if(!$this->post)
        {
            $this->error('帖子不存在');
            exit;
        }

    }

    // 查询评论数据
    public function index()
    {
        if($this->request->isPost())
        {
            $pid=$this->request->param('pid',0,'trim');
            $busid=$this->request->param('busid',0,'trim');
            $where=[
                'pid'=>$pid,
                'postid'=>$this->post->id,
            ];
            $comment=$this->CommentModel->with(['business','post'])->order('comment.createtime','desc')->where($where)->select();
            if($comment)
            {
                foreach($comment as $key=>$item)
                {
                    $comment[$key]['like_status'] = false;

                    //判断当前登录的人是否有点过赞
                    if(!empty($item['like']))
                    {
                        $arr = explode(',', $item['like']);
                        if(in_array($busid, $arr))
                        {
                            $comment[$key]['like_status'] = true;
                        }
                    }
                }
            }
            $this->success('返回评论数据',null,$comment);
            exit;
        }
    }

    // 添加评论
    public function add()
    {
        if($this->request->isPost())
        {
            $params=$this->request->param();
            // halt($params);
            
            $result=$this->CommentModel->validate('common/Post/Comment')->save($params);
            if($result===FALSE)
            {
                $this->error($this->CommentModel->getError());
                exit;
            }
            if(!$params['pid'])
            {
                $where=[
                    'pid'=>$params['pid'],
                    'postid'=>$this->post->id,
                ];
                $comment=$this->CommentModel->with(['business'])->where($where)->select();
                $this->success('回答成功',null,$comment);
                exit;
            }
            
            $this->success('回答成功');
            exit;
            
        }
    }

    public function del()
    {
        if($this->request->isPost())
        {
            $commentid=$this->request->param('commentid',0,'trim');
            $delid=$this->request->param('delid',0,'trim');
            if($delid)
            {
                $commentid=$delid;
            }
            // halt($commentid);
            $comment=$this->CommentModel->find($commentid);
            if(!$comment)
            {
                $this->error('该评论不存在');
                exit;
            }
            $result=$this->roll($commentid);
            // $result=implode(',',$result);
            // halt($result);
            // 结果
            $ComResult=$this->CommentModel->where(['id'=>$result])->delete($result);
            if($ComResult)
            {
                $this->success('评论删除成功');
                exit;
            }else
            {
                $this->error('评论删除失败');
            }
        }
    }

    // 递归删除
    public function roll($pid,$list=[])
    {
        if($this->request->isPost())
        {
            $rows=$this->CommentModel->where(['pid'=>$pid])->select();

            $list[]=$pid;

            if($rows)
            {
                // 有子评论,循环子集评论
                foreach($rows as $item)
                {
                    // 递归
                    $list=$this->roll($item['id'],$list);
                }
            }
            return $list;
        }
    }

    // 点赞和取消点赞
    public function like()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $commentid=$this->request->param('commentid',0,'trim');
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error("用户不存在");
                exit;
            }

            $like=$this->CommentModel->where(['id'=>$commentid])->value('like');
            
            $arr=explode(',',$like);
           
            if(in_array($busid,$arr))
            {
                // 取消点赞
                $index = array_search($busid, $arr);
                unset($arr[$index]);
                
                $update=implode(',', $arr);//将数组转换为字符串

                // 更新数据
                $data=[
                    'id' => $commentid,
                    'like' => $update, 
                ];

                // 更新语句
                $result = $this->CommentModel->isUpdate(true)->save($data);
                if(!$result)
                {
                    $this->error('取消点赞失败');
                    exit;
                }else
                {
                    $this->success('取消点赞成功');
                    exit;
                }
            }else
            {
                // 点赞
                array_unshift($arr,$busid);
                $update=trim(implode(',', $arr),',');//将数组转换为字符串,并去掉两边的逗号
                $data=[
                    'id' => $commentid,
                    'like' => $update, 
                ];


                // 更新语句
                $result = $this->CommentModel->isUpdate(true)->save($data);
                if(!$result)
                {
                    $this->error('点赞失败');
                    exit;
                }else
                {
                    $this->success('点赞成功');
                    exit;
                }
            }

            
        }
    }

    public function accept()
    {
        if($this->request->isPost())
        {
            $acceptid=$this->request->param('acceptid',0,'trim');
            $commentid=$this->request->param('commentid',0,'trim');
            $accept=$this->BusinessModel->find($acceptid);
            if(!$accept)
            {
                $this->error('采纳人不存在');
                exit;
            }
          
            $comment=$this->CommentModel->find($commentid);
            if(!$comment)
            {
                $this->error('评论不存在');
                exit;
            }
            
            
            // 采纳评论
            // 更新帖子状态和采纳人id
            // 更新帖子评论表状态
            // 更新用户余额

            // 加载模型
            $PostModel=model("Post.Post");
            $CommentModel=model("Post.Comment");
            $BusinessModel=model("Business.Business");

            // 开启事务
            $PostModel->startTrans();
            $CommentModel->startTrans();
            $BusinessModel->startTrans();

            // 更新帖子表数据
            $PostData=[
                'id'=>$this->post->id,
                'accept'=>$acceptid,
                'status'=>'1'
            ];
            $PostStatus=$PostModel->isUpdate(true)->save($PostData);
            if($PostData===FALSE)
            {
                $this->error('帖子更新失败');
                exit;
            }
            // 更新帖子评论表数据
            $CommentData=[
                'id'=>$commentid,
                'status'=>'1'
            ];
            $CommentStatus=$CommentModel->isUpdate(true)->save($CommentData);
            if($CommentStatus===FALSE)
            {
                $PostModel->rollback();
                $this->error('帖子评论更新失败');
                exit;
            }

            // 获取悬赏积分
            $point=$this->post->point;
            $money=$accept['money'];
            $update=bcadd($money,$point);
            // 更新用户表数据
            $BusinessData=[
                'id'=>$acceptid,
                'money'=>$update
            ];
            $BusinessStatus=$BusinessModel->isUpdate(true)->save($BusinessData);
            if($BusinessStatus===FALSE)
            {
                $CommentModel->rollback();
                $PostModel->rollback();
                $this->error('帖子评论更新失败');
                exit;
            }
            if($PostStatus===FALSE || $CommentStatus===FALSE || $BusinessStatus===FALSE)
            {
                $BusinessModel->rollback();
                $CommentModel->rollback();
                $PostModel->rollback();
                $this->error('采纳失败');
                exit;
            }else
            {
                $PostModel->commit();
                $CommentModel->commit();
                $BusinessModel->commit();
                $this->success('采纳成功');
                exit;
            }

        }
    }
    



   
}